

local CustomClock = CustomClock;




do
	local ccFrame = CustomClock_DisplayFrame;
	
	ccFrame:RegisterEvent("VARIABLES_LOADED");
	ccFrame:RegisterEvent("PLAYER_LOGOUT");
	
	ccFrame.TEXT = CustomClock_DisplayFrameText;
	ccFrame.CUSTOMCLOCK = CustomClock;
	CustomClock.DISPLAY_FRAME = ccFrame;
	
	ccFrame.Elapsed = 0.0;
	CustomClock.SCRIPTS_FRAME = ccFrame;
	
	
	
end



function CustomClock:GetPosInfo()
  local ReferenceMark = "|cFFFFCC33!"; 
  local ReferenceMark2 = "|cFFFFCC22!"; 
  local nullReference = "|cFF660000!";

  local subzoneText = "";
  local outstr = "|cFFFF0000This!|r";
  local inBattleGround = nullReference;
  local inZone = nullReference;
  local isOutDoors = nullReference;
  
  local isDead = nullReference;
  local inSubZone = nullReference;
  local inQueue = nullReference;





	
 if (GetZoneText() == "Darnassus" ) then
  inZone = "|cFFFAFAFA!";
 end

 if (GetZoneText() == "Stormwind City" ) then
  inZone = "|cFFFAFAAA!";
 end

 if (GetZoneText() == "IronForge" ) then
  inZone = "|cFFFAFABB!";
 end

 if (GetZoneText() == "Shattrath" ) then
  inZone = "|cFFFAFACC!";
 end
 
  if (IsOutdoors() == 1) then
   isOutDoors = "|cFF808080!"; 
   else
   isOutDoors = "|cFF666768!"; --Implied if your not outdoors your what then
  end
  
   if (UnitIsDeadOrGhost("player")) then
  isDead = "|cFF010203!";
 end

 if (GetZoneText() == "Eye of the Storm" ) then
  inZone = "|cFFF5F5F5!";
  subzoneText = GetSubZoneText();
  if (subzoneText == "Mage Tower") then
   inSubZone = "|cFFC1C2C3!";
  end
  if (subzoneText == "Draenei Ruins") then
   inSubZone = "|cFFC2C3C4!"; 
  end
  if (subzoneText == "Blood Elf Tower") then
  inSubZone = "|cFFC3C4C5!"; 
  end
  if (subzoneText == "Fel Reaver Ruins") then
  inSubZone = "|cFFC4C5C6!"; 
  end
  
 end

 if (GetZoneText() == "Warsong Gulch" ) then
  inZone = "|cFFA1A2A3!";
  subzoneText = GetSubZoneText();
    if (subzoneText == "Silverwing Hold") then
   inSubZone = "|cFFA1A2A4!";
  end

 end
 
 if (GetZoneText() == "Arathi Basin" ) then
  inZone = "|cFFB1B2B3!";
  subzoneText = GetSubZoneText();
  if (subzoneText == "Trollbane Hall") then
   inSubZone = "|cFFB2B3B4!";
  end
  if (subzoneText == "Stables") then
   inSubZone = "|cFFB3B4B5!";
  end
  if (subzoneText == "Gold Mine") then
   inSubZone = "|cFFB4B5B6!";
  end
  if (subzoneText == "Farm") then
   inSubZone = "|cFFB5B6B7!";
  end
  if (subzoneText == "Lumber Mill") then
   inSubZone = "|cFFB6B7B8!";
  end
  if (subzoneText == "Blacksmith") then
   inSubZone = "|cFFB7B8B9!";
  end

 end

  if (GetZoneText() == "Alterac Vally" ) then
  inZone = "|cFFF0F1F2!";
    subzoneText = GetSubZoneText();
  if (subzoneText == "Dun Baldar Pass") then
   inSubZone = "|cFFF1F2F3!";
  end
  if (subzoneText == "Stormpike Graveyard") then
   inSubZone = "|cFFF2F3F4!";
  end
  if (subzoneText == "Dun Baldar") then
   inSubZone = "|cFFF3F4F5!";
  end
  if (subzoneText == "Winterax Hold") then
   inSubZone = "|cFFF4F5F6!";
  end
  if (subzoneText == "Iceblood Graveyard") then
   inSubZone = "|cFFF5F6F7!";
  end
  if (subzoneText == "Stonehearth Graveyard") then
   inSubZone = "|cFFF6F7F8!";
  end
  if (subzoneText == "Frostwolf Graveyard") then
   inSubZone = "|cFFF7F8F9!";
  end
  if (subzoneText == "Frostwolf Relif Hut") then
   inSubZone = "|cFFF8F9FA!";
  end


 end

  
  
  if (GetNumRaidMembers() > 2) then
   inBattleGround = "|cFFFFFFFF!"; 
  end
  
 --In queue is in order ,  could be any number 1 - 4 since there are 4 battle grounds and you can queue for all at once.
 if (GetBattlefieldEstimatedWaitTime(0) > 0) then
  inQueue = "|FFD1D2D3!";
 end

 if (GetBattlefieldEstimatedWaitTime(1) > 0) then --AB
  inQueue = "|FFD1D2D3!";
 end

 if (GetBattlefieldEstimatedWaitTime(2) > 0) then
  inQueue = "|FFD1D2D3!";
 end
if (GetBattlefieldEstimatedWaitTime(3) > 0) then
  inQueue = "|FFD1D2D3!";
 end
if (GetBattlefieldEstimatedWaitTime(4) > 0) then
  inQueue = "|FFD1D2D3!";
 end
if (GetBattlefieldEstimatedWaitTime(5) > 0) then
  inQueue = "|FFD1D2D3!";
 end
if (GetBattlefieldEstimatedWaitTime(6) > 0) then
  inQueue = "|FFD1D2D3!";
 end

 
    return ReferenceMark  .. inBattleGround .. inZone .. isOutDoors .. isDead .. inSubZone .. inQueue;

end
function CustomClock:Main_UpdateDisplay()
	
	--self.DISPLAY_FRAME.TEXT:SetText(date(self.Config.TimePattern));
	--self.DISPLAY_FRAME.TEXT:FontString:SetFont("FRIZQT__.TTF",11,"OUTLINE, MONOCHROME")

	self.DISPLAY_FRAME.TEXT:SetText(self.GetPosInfo());
	
	
	

end




function CustomClock:Main_OnEvent(event)
	if (event == "VARIABLES_LOADED") then
		self:Config_LoadSettings();
	elseif (event == "PLAYER_LOGOUT") then
		self:Config_SaveSettings();
	end
end


CustomClock.SCRIPTS_FRAME:SetScript("OnEvent", (function(self, event, ...) self.CUSTOMCLOCK:Main_OnEvent(event); end));




CustomClock.SCRIPTS_FRAME:SetScript("OnUpdate",
  function(self, elapsed)
	self.Elapsed = (self.Elapsed + elapsed);
	
	if (self.Elapsed > 0.9) then
		self.Elapsed = 0.0;
		
		self.CUSTOMCLOCK:Main_UpdateDisplay();
	end
  end
);




function CustomClock:Main_SlashCommandHandler(msg)
	local command, argument = msg:match("([^%s]+) (.+)");
	command = (command or msg):lower();
	
	local localization = self.LOCALIZATION;
	
	
	if (command == localization.SLASH_PATTERN) then
		if (argument ~= nil and argument ~= "") then
			local dateString = self:Config_SetDisplayPattern(argument);
			if (dateString ~= nil) then
				DEFAULT_CHAT_FRAME:AddMessage(localization.NEW_PATTERN:format(argument), 0.6, 0.8, 0.6);
			else
				DEFAULT_CHAT_FRAME:AddMessage(localization.INVALID_PATTERN, 0.6, 0.8, 0.6);
			end
		else
			local chatFrame = DEFAULT_CHAT_FRAME;
			local addMessage = chatFrame.AddMessage;
			for index = 1, 23 do
				addMessage(chatFrame, localization["PATTERN_HELP_"..index], 0.6, 0.8, 0.6);
			end
			addMessage(chatFrame, localization.CURRENT_PATTERN:format(self.Config.TimePattern), 0.6, 0.8, 0.6);
		end
		
		
	elseif (command == localization.SLASH_COLOR) then
		local tonumber = tonumber;
		local mathceil = math.ceil;
		
		local redVal, greenVal, blueVal = (argument or ""):match("(%d+) (%d+) (%d+)");
		
		if (redVal ~= nil) then
			local success = self:Config_SetTextColor(mathceil(tonumber(redVal)), mathceil(tonumber(greenVal)), mathceil(tonumber(blueVal)));
			
			if (success == true) then
				DEFAULT_CHAT_FRAME:AddMessage(localization.NEW_COLORS:format(redVal, greenVal, blueVal), 0.6, 0.8, 0.6);
			else
				DEFAULT_CHAT_FRAME:AddMessage(localization.INVALID_COLORS, 0.6, 0.8, 0.6);
			end
		else
			DEFAULT_CHAT_FRAME:AddMessage(localization.INVALID_COLORS, 0.6, 0.8, 0.6);
		end
		
		
	elseif (command == localization.SLASH_FONTPATH) then
		if ((argument or ""):lower() == localization.SLASH_FONTPATH_LIST) then
			DEFAULT_CHAT_FRAME:AddMessage(localization.FONT_LIST, 0.6, 0.8, 0.6);
		else
			local success = self:Config_SetFontPath(argument);
			if (success == true) then
				DEFAULT_CHAT_FRAME:AddMessage(localization.NEW_FONT:format(argument), 0.6, 0.8, 0.6);
			else
				DEFAULT_CHAT_FRAME:AddMessage(localization.INVALID_FONT, 0.6, 0.8, 0.6);
			end
		end
		
		
	elseif (command == localization.SLASH_SIZE) then
		local success = self:Config_SetFontSize(argument);
		if (success == true) then
			DEFAULT_CHAT_FRAME:AddMessage(localization.NEW_SIZE:format(argument), 0.6, 0.8, 0.6);
		else
			DEFAULT_CHAT_FRAME:AddMessage(localization.INVALID_SIZE, 0.6, 0.8, 0.6);
		end
		
		
	elseif (command == localization.SLASH_WIDTH) then	
		local success = self:Config_SetFrameWidth(argument);
		if (success == true) then
			DEFAULT_CHAT_FRAME:AddMessage(localization.NEW_WIDTH:format(argument), 0.6, 0.8, 0.6);
		else
			DEFAULT_CHAT_FRAME:AddMessage(localization.INVALID_WIDTH, 0.6, 0.8, 0.6);
		end
		
		
	elseif (command == localization.SLASH_HEIGHT) then	
		local success = self:Config_SetFrameHeight(argument);
		if (success == true) then
			DEFAULT_CHAT_FRAME:AddMessage(localization.NEW_HEIGHT:format(argument), 0.6, 0.8, 0.6);
		else
			DEFAULT_CHAT_FRAME:AddMessage(localization.INVALID_HEIGHT, 0.6, 0.8, 0.6);
		end
		
		
	else
		local chatFrame = DEFAULT_CHAT_FRAME;
		local addMessage = chatFrame.AddMessage;
		
		for index = 1, 7 do
			addMessage(chatFrame, localization["COMMANDS_HELP_"..index], 0.6, 0.8, 0.6);
		end
	end
end


SlashCmdList["CUSTOMCLOCK"] = (function(msg) CustomClock:Main_SlashCommandHandler(msg); end);
SLASH_CUSTOMCLOCK1 = ("/cucl");

